﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;

namespace Powershell_ML
{
    public class PowerShell 
    {
        public static Runspace Current;
        public static Pipeline Commands_Pipes;
        public event EventHandler Command_Start;
        public event EventHandler Command_End;
        public static RunspaceConfiguration Config;
       
        public PowerShell()
        {
            Current = RunspaceFactory.CreateRunspace();
            Config = RunspaceConfiguration.Create();

            PSSnapInException SnapException;
            Commands_Pipes = Current.CreatePipeline();
            try
            {
                Current.Open();

            }
            catch (Exception) { }
        }
        public List<PSObject> Execute_Command(Command commands)
        {
            var Output_Data = new List<PSObject>();
            PSSnapInException SnapException;
            Config.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out SnapException);
            Commands_Pipes.Commands.Add(commands);
            Collection<PSObject> PSOutput = Commands_Pipes.Invoke();
            try
            {
                foreach (PSObject cmd in PSOutput)
                {
                    Output_Data.Add(cmd);
                }
                return Output_Data;
            }
            catch
            {
                return Output_Data;
            }


        }
        public PSObject Execute_Command_Once(Command commands)
        {
            var Output_Data = new List<PSObject>();
            var runspace = Current;
            var pipeline = Commands_Pipes;
            pipeline.Commands.Add(commands);
            Collection<PSObject> PSOutput = pipeline.Invoke();
            try
            {
                foreach (PSObject cmd in PSOutput)
                {
                    return cmd;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }

        public PSObject Execute_Scripts(string commands)
        {
            var Output_Data = new List<PSObject>();
            var runspace = Current;
            var pipeline = Commands_Pipes;
            pipeline.Commands.AddScript(commands);
            Collection<PSObject> PSOutput = pipeline.Invoke();
            try
            {
                foreach (PSObject cmd in PSOutput)
                {
                    return cmd;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }

        public Collection<PSObject> Execute_Commands(Command cmd)
        {
            if (cmd != null)
            {
                try
                {
                    Commands_Pipes.Commands.Add(cmd);
                    Collection<PSObject> Output = Commands_Pipes.Invoke();
                    return Output;
                }
                catch (Exception) { return null; }
            }
            else
            {
                return null;
            }
        }
        public bool Work_Space_Opened()
        {
            if (Current.RunspaceAvailability == RunspaceAvailability.Available)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
